package com.aaa.tmr.service.impl;

import com.aaa.tmr.entity.Custom;
import com.aaa.tmr.dao.CustomDao;
import com.aaa.tmr.listener.DataListener;
import com.aaa.tmr.service.CustomService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Custom)表服务实现类
 *
 * @author makejava
 * @since 2021-03-19 15:33:36
 */
@Service("customService")
public class CustomServiceImpl implements CustomService {
    @Resource
    private CustomDao customDao;

    /**
     * 通过ID查询单条数据
     *
     * @param customId 主键
     * @return 实例对象
     */
    @Override
    public Custom queryById(Integer customId) {
        return this.customDao.queryById(customId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Custom> queryAllByLimit(int offset, int limit) {
        return this.customDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param custom 实例对象
     * @return 实例对象
     */
    @Override
    public Custom insert(Custom custom) {
        this.customDao.insert(custom);
        return custom;
    }

    /**
     * 修改数据
     *
     * @param custom 实例对象
     * @return 实例对象
     */
    @Override
    public Custom update(Custom custom) {
        this.customDao.update(custom);
        return this.queryById(custom.getCustomId());
    }

    /**
     * 通过主键删除数据
     *
     * @param customId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer customId) {
        return this.customDao.deleteById(customId) > 0;
    }

    @Override
    public PageInfo queryAll(int pageNo, int pageSize, Custom custom) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<Custom>(this.customDao.queryAll(custom));
    }

    @Override
    public int batchDeleteById(String customIds) {
        return customDao.batchDeleteById(customIds);
    }

    @Override
    public boolean importCustom(MultipartFile file) {
        ExcelReader excelReader = null;
        try {
            //实例化监听对象
            DataListener dataListener =new DataListener();
            //读excel数据到excelReader对象中
            excelReader = EasyExcel.read(file.getInputStream(), Custom.class,dataListener).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            //定义标识符
            boolean isSuc =true;
            //获取读取到的数据列表
            List list =  dataListener.getList();
            //判断是否为空
            if(!CollectionUtils.isEmpty(list)){
                //循环对象
                for (Object o : list) {
                    Custom custom =(Custom)o;
                    Custom returnCustom = this.insert(custom);
                    if(returnCustom==null){
                        isSuc=false;
                    }
                }
            }
            return isSuc;
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        return false;
    }
}
